# Maintainer: Oleg Titov <oleg.titov@gmail.com>

pkgbase=brotli
pkgname=('brotli' 'brotli-devel' 'python-brotli' 'python2-brotli' 'brotli-testdata')
pkgver=1.0.5
pkgrel=2
pkgdesc='Brotli compression library'
arch=('i686' 'x86_64')
license=('MIT')
url='https://github.com/google/brotli'
depends=('msys2-runtime' 'gcc-libs')
makedepends=('cmake' 'python' 'python2')
source=("${pkgbase}-${pkgver}.tar.gz::https://github.com/google/$pkgbase/archive/v${pkgver}.tar.gz"
        brotly-rename-static-libs.patch)
sha256sums=('3d5bedd48edb909fe3b87cb99f7d139b987ef6f1616b7e22d74e928270a2fd20'
            '25c2e154e805f6bd2999343a3d38efefa213577e09f5ea932bfa7eb42cf97f35')

prepare() {
  cd brotli-${pkgver}
  patch -p1 -i ${srcdir}/brotly-rename-static-libs.patch
  cd ..
  cp -a brotli-${pkgver}{,-py2}
  mkdir -p build
}

build() {
  cd "${srcdir}"/brotli-${pkgver}
  python setup.py build

  cd "${srcdir}"/brotli-${pkgver}-py2
  python2 ./setup.py build

  cd "${srcdir}"/build
  cmake \
    -DCMAKE_INSTALL_PREFIX="/usr" \
    -DCMAKE_INSTALL_LIBDIR="/usr/lib" \
    ../brotli-${pkgver}

  make
}

check() {
  cd brotli-${pkgver}
  make test
}

package_brotli() {
  cd build
  make DESTDIR=${pkgdir} install
  rm -rf ${pkgdir}/usr/{include,lib}
  install -D -m644 "${srcdir}"/brotli-${pkgver}/LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

package_brotli-devel() {
  depends=('brotli')
  cd build
  make DESTDIR=${pkgdir} install
  rm -rf ${pkgdir}/usr/bin
}

package_python-brotli() {
  depends=('python')

  cd brotli-${pkgver}

  python setup.py install --skip-build -O1 --root="${pkgdir}"
  install -D -m644 "${srcdir}"/brotli-${pkgver}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_python2-brotli() {
  depends=('python2')

  cd brotli-${pkgver}-py2

  python2 setup.py install --skip-build -O1 --root="${pkgdir}"
  install -D -m644 "$srcdir"/brotli-$pkgver/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_brotli-testdata() {
  depends=()

  cd brotli-${pkgver}
  install -dm755 "${pkgdir}"/usr/share/brotli
  cp -a tests/testdata "${pkgdir}"/usr/share/brotli/
  install -D -m644 "${srcdir}"/brotli-${pkgver}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}
